<!--
  ~  Licensed to the Apache Software Foundation (ASF) under one or more
  ~  contributor license agreements.  See the NOTICE file distributed with
  ~  this work for additional information regarding copyright ownership.
  ~  The ASF licenses this file to You under the Apache License, Version 2.0
  ~  (the "License"); you may not use this file except in compliance with
  ~  the License.  You may obtain a copy of the License at
  ~
  ~     http://www.apache.org/licenses/LICENSE-2.0
  ~
  ~  Unless required by applicable law or agreed to in writing, software
  ~  distributed under the License is distributed on an "AS IS" BASIS,
  ~  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  ~  See the License for the specific language governing permissions and
  ~  limitations under the License.
  -->
<div class="flex flex-col h-full gap-y-2">
    <div class="flex-1">
        <ng-container>
            <div class="connection-status-table h-full flex flex-col">
                <!-- allow filtering of the table -->
                <summary-table-filter
                    [filteredCount]="filteredCount"
                    [totalCount]="totalCount"
                    [filterableColumns]="filterableColumns"
                    [includeStatusFilter]="false"
                    [includePrimaryNodeOnlyFilter]="false"
                    filterColumn="sourceName"
                    [clusterNodes]="clusterNodes"
                    [selectedNode]="selectedClusterNode"
                    (filterChanged)="applyFilter($event)"></summary-table-filter>

                <div class="flex-1 relative">
                    <div class="listing-table overflow-y-auto absolute inset-0">
                        <table
                            mat-table
                            [dataSource]="dataSource"
                            matSort
                            matSortDisableClear
                            (matSortChange)="sortData($event)"
                            [matSortActive]="initialSortColumn"
                            [matSortDirection]="initialSortDirection">
                            <!-- Name Column -->
                            <ng-container matColumnDef="name">
                                <th mat-header-cell *matHeaderCellDef mat-sort-header>
                                    <div class="overflow-ellipsis overflow-hidden whitespace-nowrap">Name</div>
                                </th>
                                <td mat-cell *matCellDef="let item" [title]="formatName(item)">
                                    {{ formatName(item) }}
                                </td>
                            </ng-container>

                            <!-- Queued column -->
                            <ng-container matColumnDef="queue">
                                <th
                                    mat-header-cell
                                    *matHeaderCellDef
                                    mat-sort-header
                                    title="Count / data size in the last 5 minutes">
                                    <div
                                        class="inline-block overflow-hidden overflow-ellipsis whitespace-nowrap space-x-1">
                                        <span
                                            [ngClass]="{
                                                underline:
                                                    multiSort.active === 'queue' && multiSort.sortValueIndex === 0
                                            }"
                                            >Queue</span
                                        >
                                        <span
                                            [ngClass]="{
                                                underline:
                                                    multiSort.active === 'queue' && multiSort.sortValueIndex === 1
                                            }"
                                            >(Size)</span
                                        >
                                        <span class="font-light">5 min</span>
                                    </div>
                                </th>
                                <td mat-cell *matCellDef="let item" [title]="formatQueue(item)">
                                    {{ formatQueue(item) }}
                                </td>
                            </ng-container>

                            <!-- Threshold column -->
                            <ng-container matColumnDef="threshold">
                                <th
                                    mat-header-cell
                                    *matHeaderCellDef
                                    mat-sort-header
                                    title="Percent of threshold used for count and data size">
                                    <div
                                        class="inline-block overflow-hidden overflow-ellipsis whitespace-nowrap space-x-1">
                                        <span>Threshold %:</span>
                                        <span
                                            [ngClass]="{
                                                underline:
                                                    multiSort.active === 'threshold' && multiSort.sortValueIndex === 0
                                            }"
                                            >Queue</span
                                        >
                                        <span>|</span>
                                        <span
                                            [ngClass]="{
                                                underline:
                                                    multiSort.active === 'threshold' && multiSort.sortValueIndex === 1
                                            }"
                                            >Size</span
                                        >
                                    </div>
                                </th>
                                <td mat-cell *matCellDef="let item" [title]="formatThreshold(item)">
                                    {{ formatThreshold(item) }}
                                </td>
                            </ng-container>

                            <!-- Input column -->
                            <ng-container matColumnDef="in">
                                <th
                                    mat-header-cell
                                    *matHeaderCellDef
                                    mat-sort-header
                                    title="Count / data size in the last 5 minutes">
                                    <div
                                        class="inline-block overflow-hidden overflow-ellipsis whitespace-nowrap space-x-1">
                                        <span
                                            [ngClass]="{
                                                underline: multiSort.active === 'in' && multiSort.sortValueIndex === 0
                                            }"
                                            >In</span
                                        >
                                        <span
                                            [ngClass]="{
                                                underline: multiSort.active === 'in' && multiSort.sortValueIndex === 1
                                            }"
                                            >(Size)</span
                                        >
                                        <span class="font-light">5 min</span>
                                    </div>
                                </th>
                                <td mat-cell *matCellDef="let item" [title]="formatIn(item)">
                                    {{ formatIn(item) }}
                                </td>
                            </ng-container>

                            <!-- Source Column -->
                            <ng-container matColumnDef="sourceName">
                                <th mat-header-cell *matHeaderCellDef mat-sort-header>
                                    <div class="overflow-ellipsis overflow-hidden whitespace-nowrap">From Source</div>
                                </th>
                                <td mat-cell *matCellDef="let item" [title]="formatSource(item)">
                                    {{ formatSource(item) }}
                                </td>
                            </ng-container>

                            <!-- Output column -->
                            <ng-container matColumnDef="out">
                                <th
                                    mat-header-cell
                                    *matHeaderCellDef
                                    mat-sort-header
                                    title="Count / data size in the last 5 minutes">
                                    <div
                                        class="inline-block overflow-hidden overflow-ellipsis whitespace-nowrap space-x-1">
                                        <span
                                            [ngClass]="{
                                                underline: multiSort.active === 'out' && multiSort.sortValueIndex === 0
                                            }"
                                            >Out</span
                                        >
                                        <span
                                            [ngClass]="{
                                                underline: multiSort.active === 'out' && multiSort.sortValueIndex === 1
                                            }"
                                            >(Size)</span
                                        >
                                        <span class="font-light">5 min</span>
                                    </div>
                                </th>
                                <td mat-cell *matCellDef="let item" [title]="formatOut(item)">
                                    {{ formatOut(item) }}
                                </td>
                            </ng-container>

                            <!-- Destination Column -->
                            <ng-container matColumnDef="destinationName">
                                <th mat-header-cell *matHeaderCellDef mat-sort-header>
                                    <div class="overflow-ellipsis overflow-hidden whitespace-nowrap">
                                        To Destination
                                    </div>
                                </th>
                                <td mat-cell *matCellDef="let item" [title]="formatDestination(item)">
                                    {{ formatDestination(item) }}
                                </td>
                            </ng-container>

                            <!-- Load Balance Status Column -->
                            <ng-container matColumnDef="loadBalanceStatus">
                                <th mat-header-cell *matHeaderCellDef mat-sort-header>
                                    <div class="overflow-ellipsis overflow-hidden whitespace-nowrap">
                                        Load Balancing
                                    </div>
                                </th>
                                <td mat-cell *matCellDef="let item" [title]="formatLoadBalanceStatus(item)">
                                    {{ formatLoadBalanceStatus(item) }}
                                </td>
                            </ng-container>

                            <ng-container matColumnDef="actions">
                                <th mat-header-cell *matHeaderCellDef></th>
                                <td mat-cell *matCellDef="let item">
                                    <div class="flex items-center justify-end gap-x-2">
                                        <button
                                            mat-icon-button
                                            type="button"
                                            [matMenuTriggerFor]="actionMenu"
                                            class="h-16 w-16 flex items-center justify-center icon global-menu">
                                            <i class="fa fa-ellipsis-v"></i>
                                        </button>
                                        <mat-menu #actionMenu="matMenu" xPosition="before">
                                            <button
                                                mat-menu-item
                                                [routerLink]="getConnectionLink(item)"
                                                (click)="$event.stopPropagation()">
                                                <i class="fa fa-long-arrow-right primary-color mr-2"></i>
                                                Go To Connection
                                            </button>
                                            <button mat-menu-item (click)="viewStatusHistoryClicked(item)">
                                                <i class="fa fa-area-chart primary-color mr-2"></i>
                                                View Status History
                                            </button>

                                            @if (connectedToCluster) {
                                                <button mat-menu-item (click)="viewClusteredDetailsClicked(item)">
                                                    <i class="fa fa-cubes primary-color mr-2"></i>
                                                    View Clustered Details
                                                </button>
                                            }
                                        </mat-menu>
                                    </div>
                                </td>
                            </ng-container>

                            <tr mat-header-row *matHeaderRowDef="displayedColumns; sticky: true"></tr>
                            <tr
                                mat-row
                                *matRowDef="let row; let even = even; columns: displayedColumns"
                                [class.even]="even"
                                (click)="select(row)"
                                [class.selected]="isSelected(row)"></tr>
                        </table>
                    </div>
                </div>
            </div>
        </ng-container>
    </div>
    <div class="flex justify-between align-middle">
        <div class="text-sm flex items-center gap-x-2">
            <button mat-icon-button class="primary-icon-button" (click)="refresh.next()">
                <i class="fa fa-refresh" [class.fa-spin]="summaryListingStatus === 'loading'"></i>
            </button>
            <div>Last updated:</div>
            <div class="tertiary-color font-medium">{{ loadedTimestamp }}</div>
        </div>
        <div>
            <mat-paginator
                [pageSize]="100"
                [hidePageSize]="true"
                [showFirstLastButtons]="true"
                (page)="paginationChanged()"></mat-paginator>
        </div>
    </div>
</div>
